/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.io.File;
import java.io.FileReader;
import java.io.StringWriter;
import java.util.Formatter;

import net.bioteam.appweb.Config;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import static net.bioteam.appweb.submit.BtbatchPreprocessor.*;

public class MpibatchGenerator implements CommandGenerator
{
	public static final String NAME="MPIBATCH";
	public String getName()
	{
		return NAME;
	}
	public String generateScript(ProgramValueBean bean, UnfinishedJob job) throws Exception
	{
		File taskinputdir = new File(job.getWorkdir() + File.separator + inputdir);
		taskinputdir.mkdir();

		File inputfile = new File(bean.getValue("query"));
		// each sequence has 3 fasta sequences by default
		int split = 3;
		if (bean.getValue(split_key) != null)
		{
			split = Integer.parseInt(bean.getValue(split_key));
		}

		int totalSeqs=countSeqs(new FileReader(inputfile));
		if (totalSeqs/split>500)
		{
			if (totalSeqs%500!=0)
			{
				split=totalSeqs/500+1;
			} else
			{
				split=totalSeqs/500;
			}
			
		}
		int numoffiles = splitInputs(new FileReader(inputfile), taskinputdir, split);

		File taskoutputdir = new File(job.getWorkdir() + File.separator + outputdir);
		taskoutputdir.mkdir();

		CommandGenerator generator=Config.generator;
		
		
		
		String originalOutput = bean.getValue("txtoutput");
		if (originalOutput == null)
		{
			originalOutput = "blastall.txt";
		}
		//bean.setValue("_originaloutput", originalOutput);
		bean.setValue("txtoutput", outputdir + File.separator + "$i" );
		bean.setValue("query", inputdir + File.separator + "$i");
		String cmdline=generator.generateScript(bean, job);
		String tokens[]=cmdline.split(" ");
		String binaryname=tokens[0];
		StringBuffer sb=new StringBuffer();
		for (int i=1; i<tokens.length; i++)
		{
			sb.append(" ");
			sb.append(tokens[i]);
		}
		cmdline = bean.getValue(CommandSubmitter.MPIPREFIX) + " $APP" + sb.toString();
		StringBuffer sb1=new StringBuffer();
		Formatter formatter=new Formatter(sb1);
		formatter.format("APP=`which %s`\n", binaryname);
		formatter.format("let i=1\nwhile [ $i -le %d ];\ndo\n\t%s\n\tlet i=$i+1\ndone\n",numoffiles, cmdline);
		formatter.format("let i=1\nwhile [ $i -le %d ];\ndo\n\tcat %s/$i >>%s\n\tlet i=$i+1\ndone\n",numoffiles, outputdir, originalOutput);
		
		return sb1.toString();
	}
	
	 public boolean postProcessMPI()
	 {
		 return false;
	 }

	
}
